package utils

import (
	"brood-agent2/conf"
	"fmt"
	"github.com/rifflock/lfshook"
	"github.com/sirupsen/logrus"
	"os"
	"time"
)

func getLogPath() string {
	currentDate := time.Now().Format("2006-01-02")
	logName := fmt.Sprintf("logs/brood-agent2.log.%s", currentDate)
	return logName
}

func Log() *logrus.Logger {
	logger := logrus.New()
	logger.SetLevel(conf.Conf.LogLevel)
	logger.SetOutput(os.Stdout)
	logger.SetReportCaller(true)
	logger.SetFormatter(&logrus.TextFormatter{
		TimestampFormat: "2006-01-02 15:03:04",
		FullTimestamp:   true,
		ForceColors:     true,
	})

	logFilePath := getLogPath() // 指定日志文件的路径
	hook := lfshook.NewHook(
		lfshook.PathMap{
			logrus.DebugLevel: logFilePath,
			logrus.InfoLevel:  logFilePath,
			logrus.WarnLevel:  logFilePath,
			logrus.ErrorLevel: logFilePath,
			logrus.FatalLevel: logFilePath,
			logrus.PanicLevel: logFilePath,
		}, &logrus.TextFormatter{},
	)
	logger.AddHook(hook)
	return logger
}

var Logger = Log()
